//
//  ViewController.m
//  MyTest000
//
//  Created by whz on 17/1/13.
//  Copyright © 2017年 whz. All rights reserved.
//

#import "ViewController.h"

#define KCELLID @"cellID"
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kMenuTag 201712
#define kCoverViewTag 201722
#define kMargin 8
#define kTriangleHeight 10 // 三角形的高
#define kRadius 5 // 圆角半径
#define KDefaultMaxValue 6  // 菜单项最大值

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, weak)UIButton *btn;
@property(nonatomic, weak)UIView *showView;

@end

@implementation ViewController
{
    UIView *_backView;
    CGFloat _arrowPointX;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setupUI];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 15;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:KCELLID forIndexPath:indexPath];
    cell.textLabel.text = @"whz";
    
    return cell;
}

- (void)setupUI {
    
    self.view.backgroundColor = [UIColor colorWithRed:231 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1.0];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 60, 30)];
    btn.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:btn];
    self.btn = btn;
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)btnClick {
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 400)];
    view.backgroundColor = [UIColor whiteColor];
    self.showView = view;
    
    self.tableView = [[UITableView alloc] initWithFrame:view.bounds];
    [self.showView addSubview:self.tableView];
    [self.tableView setContentInset:UIEdgeInsetsMake(20, 0, 20, 0)];
        
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:KCELLID];
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    backView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
    [backView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]];
    backView.alpha = 0;
    backView.tag = 201701;
    _backView = backView;
    
//    [self.view addSubview:backView];
//    [self.view addSubview:self.showView];
    
    [[UIApplication sharedApplication].keyWindow addSubview:backView];
    [[UIApplication sharedApplication].keyWindow addSubview:view];
    
    CGPoint point = CGPointMake(0, 60);
//    point = [self.showView.superview convertPoint:point toView:self.showView.window];
//    self.showView.layer.affineTransform = CGAffineTransformIdentity;
//    [self adjustPosition:point]; // 调整展示的位置 - frame
    
//    self.showView.x = point.x - self.showView.width * 0.5;
//    self.showView.y = point.y + 8;
    self.showView.x = point.x;
    self.showView.y = point.y;
//    if (self.showView.x < 8) {
//        self.showView.x = 8;
//    }else if (self.showView.x > [UIScreen mainScreen].bounds.size.width - 8 - self.showView.width){
//        self.showView.x = [UIScreen mainScreen].bounds.size.width - 8 - self.showView.width;
//    }
    self.showView.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);

    
    // 调整箭头位置
//    if (point.x <= 8 + 5 + [UIScreen mainScreen].bounds.size.height * 0.7) {
//        _arrowPointX = 8 + 5;
//    }else if (point.x >= [UIScreen mainScreen].bounds.size.width - 8 - 5 - [UIScreen mainScreen].bounds.size.height * 0.7){
//        _arrowPointX = self.showView.width - 8 - 5;
//    }else{
//        _arrowPointX = point.x - self.showView.x;
//    }
    _arrowPointX = -10;
    
    // 调整anchorPoint
//    CGPoint aPoint = CGPointMake(0.5, 0.5);
//    if (CGRectGetMaxY(self.showView.frame) > [UIScreen mainScreen].bounds.size.height) {
//        aPoint = CGPointMake(_arrowPointX / self.showView.width, 1);
//    }else{
//        aPoint = CGPointMake(_arrowPointX / self.showView.width, 0);
//    }
    CGPoint aPoint = CGPointMake(0.0, 0.0);
    
    // 调整layer
    CAShapeLayer *layer = [self getBorderLayer];
    if (self.showView.max_Y> kScreenHeight) {
        layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
        layer.transform = CATransform3DRotate(layer.transform, M_PI, 0, 0, 1);
        self.showView.y = point.y - self.showView.height;
    }
    
    // 调整frame
    CGRect rect = self.showView.frame;
    self.showView.layer.anchorPoint = aPoint;
    self.showView.frame = rect;
    
    self.showView.layer.mask = layer;
    self.showView.layer.affineTransform = CGAffineTransformMakeScale(0.01, 0.01);
    [UIView animateWithDuration:0.25 animations:^{
        self.showView.alpha = 1;
        _backView.alpha = 0.3;
        self.showView.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
    }];
}

- (void)tap:(UITapGestureRecognizer *)sender{
    if (self.backViewTapBlock) {
        self.backViewTapBlock();
    }
    [self hiddenMenu];
    
}
- (void)hiddenMenu{
    //    self.contentTableView.contentOffset = CGPointMake(0, 0);
    [UIView animateWithDuration:0.25 animations:^{
        self.showView.layer.affineTransform = CGAffineTransformMakeScale(0.01, 0.01);
        self.showView.alpha = 0;
        _backView.alpha = 0;
    }];
}

- (void)adjustPosition:(CGPoint)point{
    self.showView.x = point.x - self.showView.width * 0.5;
    self.showView.y = point.y + 8;
    if (self.showView.x < 8) {
        self.showView.x = 8;
    }else if (self.showView.x > [UIScreen mainScreen].bounds.size.width - 8 - self.showView.width){
        self.showView.x = [UIScreen mainScreen].bounds.size.width - 8 - self.showView.width;
    }
    self.showView.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
}

- (CAShapeLayer *)getBorderLayer{
    // 上下左右的圆角中心点
    CGPoint upperLeftCornerCenter = CGPointMake(kRadius, kTriangleHeight + kRadius);
    CGPoint upperRightCornerCenter = CGPointMake(self.showView.width - kRadius, kTriangleHeight + kRadius);
    CGPoint bottomLeftCornerCenter = CGPointMake(kRadius, self.showView.height - kTriangleHeight - kRadius);
    CGPoint bottomRightCornerCenter = CGPointMake(self.showView.width - kRadius, self.showView.height - kTriangleHeight - kRadius);
    
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.frame = self.showView.bounds;
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:CGPointMake(0, kTriangleHeight + kRadius)];
    [bezierPath addArcWithCenter:upperLeftCornerCenter radius:kRadius startAngle:M_PI endAngle:M_PI * 3 * 0.5 clockwise:YES];
    [bezierPath addLineToPoint:CGPointMake(_arrowPointX - kTriangleHeight * 0.7, kTriangleHeight)];
    [bezierPath addLineToPoint:CGPointMake(_arrowPointX, 0)];
    [bezierPath addLineToPoint:CGPointMake(_arrowPointX + kTriangleHeight * 0.7, kTriangleHeight)];
    [bezierPath addLineToPoint:CGPointMake(self.showView.width - kRadius, kTriangleHeight)];
    [bezierPath addArcWithCenter:upperRightCornerCenter radius:kRadius startAngle:M_PI * 3 * 0.5 endAngle:0 clockwise:YES];
    [bezierPath addLineToPoint:CGPointMake(self.showView.width, self.showView.height - kTriangleHeight - kRadius)];
    [bezierPath addArcWithCenter:bottomRightCornerCenter radius:kRadius startAngle:0 endAngle:M_PI_2 clockwise:YES];
    [bezierPath addLineToPoint:CGPointMake(kRadius, self.showView.height - kTriangleHeight)];
    [bezierPath addArcWithCenter:bottomLeftCornerCenter radius:kRadius startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
    [bezierPath addLineToPoint:CGPointMake(0, kTriangleHeight + kRadius)];
    [bezierPath closePath];
    borderLayer.path = bezierPath.CGPath;
    
    return borderLayer;
}

- (UITableView *)tableView {

    if (!_tableView) {
        
        _tableView = [UITableView new];
    }
    return _tableView;
}




//    UIView *singleMapView = [[UIView alloc]
//                             initWithFrame:CGRectMake(10, 10, self.view.bounds.size.width, 250)];
//    singleMapView.backgroundColor = [UIColor orangeColor];
//    [self.view addSubview:singleMapView];
//    singleMapView.transform = CGAffineTransformMakeScale(0.05, 0.05);
//    [UIView animateWithDuration:3
//                     animations:^{
//                         singleMapView.transform = CGAffineTransformMakeScale(1.2, 1.2);
//                     }completion:^(BOOL finish){
//                         [UIView animateWithDuration:3
//                                          animations:^{
//                                              singleMapView.transform = CGAffineTransformMakeScale(0.9, 0.9);
//                                          }completion:^(BOOL finish){
//                                              [UIView animateWithDuration:3
//                                                               animations:^{
//                                                                   singleMapView.transform = CGAffineTransformMakeScale(1, 1);
//                                                               }completion:^(BOOL finish){
//
//                                                               }];
//                                          }];
//                     }];


//- (void) shakeToShow:(UIView*)aView{
//    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
//    animation.duration = 3.5;
//
//    NSMutableArray *values = [NSMutableArray array];
//    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
//    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
//    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
//    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
//    animation.values = values;
//    [aView.layer addAnimation:animation forKey:nil];
//}

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//
//    return 1;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//
//    return 5;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:KCELLID forIndexPath:indexPath];
//
//    cell.textLabel.text = @"whz";
//
//    return cell;
//}
//
//- (void)setupUI {
//
//    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
//    self.tableView = tableView;
//    [self.view addSubview:tableView];
//
//    self.tableView.dataSource = self;
//    self.tableView.delegate = self;
//
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:KCELLID];
//}


@end
